package com.baoli.blzj;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

import com.baoli.blzj.adapter.TabFragmentPagerAdapter;
import com.baoli.blzj.customviews.CustomHomeTitleBar;
import com.baoli.blzj.ui.fragment.BaseFragment;
import com.baoli.blzj.ui.fragment.HomeFragment;
import com.baoli.blzj.ui.home.HomeArrangeGnActivity;
import com.viewpagerindicator.TabPageIndicator;

/**
 * 首页面 by John
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {

    public TabFragmentPagerAdapter pagerAdapter;

    public ViewPager viewPager;

    public TabPageIndicator indicator;

    private PopupWindow popupWindow;

    private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //popupWindow();
    }

    /**
     * 天气情况弹窗
     */
    public void popupWindow() {
        inflater = LayoutInflater.from(this);
        View layout = inflater.inflate(R.layout.popupwindow_index, null);
        popupWindow = new PopupWindow(layout, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnimationStyle(R.style.TopSelectAnimationShow);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new PaintDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });
    }

    /**
     * 初始化视图
     */
    private void initView() {
        //初始化导航栏
        new CustomHomeTitleBar(this).setTitleText(getResStr(R.string.index)).setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //new CustomPopupTopView(MainActivity.this);
                popupWindow.showAsDropDown(findViewById(R.id.title_text));
                popupWindow.update();
                //popupWindow.showAsDropDown(v);
            }
        }).setRightIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, HomeArrangeGnActivity.class);
                startActivity(intent);
            }
        });
        viewPager = (ViewPager) findViewById(R.id.pager);
        indicator = (TabPageIndicator) findViewById(R.id.indicator);
        initViewPager();
        indicator.setVisibility(View.VISIBLE);
        indicator.setViewPager(viewPager);
        indicator.setCurrentItem(0);
    }

    /**
     * 实例化fragment并添加到ViewPager
     */
    private void initViewPager() {

        pagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager());

        pagerAdapter.getFragments().add(new HomeFragment());

        pagerAdapter.getFragments().add(new BaseFragment());

        pagerAdapter.getFragments().add(new BaseFragment());

        pagerAdapter.getFragments().add(new BaseFragment());

        viewPager.setOffscreenPageLimit(pagerAdapter.getCount());

        viewPager.setAdapter(pagerAdapter);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
        }
    }
}
